#!/usr/bin/env python
# encoding=utf-8

import sys
from pymongo import MongoClient
reload(sys)
sys.setdefaultencoding('utf-8')

__author__ = 'xlzd'

# CONN = MongoClient('192.168.31.121')
CONN = MongoClient('10.132.23.104', 27017)


def select(coll, filter=None, sort_key='updateTime'):
    data = CONN['crawler_company_all'][coll].find(filter).sort(sort_key, -1)
    return data


def stock_2_company_name(stock):
    try:
        return CONN['crawler_company_all']['stockCode'].find_one({'_id': stock})['companyName']
    except:
        return ''


def key_2_stock(key):
    try:
        if key.isdigit():
            return key
        if len(key) <= 5:
            data = CONN['crawler_company_all']['stockCode'].find_one({'companyShortName': key})
            if data:
                return data['_id']
        return CONN['crawler_company_all']['stockCode'].find_one({'companyName': key})['_id']
    except:
        return ''


if __name__ == '__main__':
    print select('stockAnnouncement').count()
